<script setup>
import pageCom from '@/components/pagecontainer.vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router';
import { userStore } from '@/stores';
import { userUpdatePass } from '@/api/user'
const title = ref('修改密码')
const form = ref()
const formModel = ref({
  old_pwd: "",
  new_pwd: "",
  re_pwd: "",
})
const rules = ref({
  old_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { pattern: /^\S[a-zA-Z0-9]{6,10}$/, message: '长度在6到10之间', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { pattern: /^\S[a-zA-Z0-9]{6,10}$/, message: '长度在6到10之间', trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { pattern: /^\S[a-zA-Z0-9]{6,10}$/, message: '长度在6到10之间', trigger: 'blur' },
    {
      validator: (rules, value, callback) => {
        if (value !== formModel.value.newpass) {
          callback('两次密码输入不一致')
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
})
const user = userStore()
const router = useRouter()
const updatepass = async () => {
  await form.value.validate()
  await userUpdatePass(formModel.value)
  user.setToken(''),
    user.setUser({})
  ElMessage.success('修改成功')
  router.push('/login')
}
const Reset = () => {
  form.value.resetFields()
}
</script>

<template>
  <pageCom :title="title">
    <el-form ref="form" :model="formModel" :rules="rules" class="form" label-width="150px">
      <el-form-item label="原密码 " prop="old_pwd">
        <el-input v-model="formModel.old_pwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码 " prop="new_pwd">
        <el-input v-model="formModel.new_pwd"></el-input>
      </el-form-item>
      <el-form-item label="用确认新密码 " prop="re_pwd">
        <el-input v-model="formModel.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatepass">提交修改</el-button>
        <el-button type="info" @click="Reset">重置</el-button>
      </el-form-item>
    </el-form>
  </pageCom>
</template>
<style lang="scss" scope>
.form {
  margin: 0 auto;
  width: 80%;
}
</style>